/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

import java.util.ArrayList;

public class ActivityStructuresTranslator {

    /**
     * Creates a new instance of ActivityStrucuctureTranslator    
     * @param parent The activity class
     * @param node The activity structure node of the Dom structure
     * @param ont The ontology
     * @param instLD The activity instance
     * @param nsName The namespace
     */
    public ActivityStructuresTranslator(OntClass parent, Node node,
            OntModel ont,
            Individual instLD, String nsName, ArrayList references,
            ArrayList referencesEnvironment, ArrayList referencesSupport,
            ArrayList referencesLearningActivity) {
        lastInstanceSLDTranslator lastInst =
                new lastInstanceSLDTranslator();


        ObjectProperty exeEntRef =
                ont.getObjectProperty(nsName + "execution-entity-ref");
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty execution =
                ont.getDatatypeProperty(nsName + "execution-order");
        DatatypeProperty number =
                ont.getDatatypeProperty(nsName + "number-to-select");
        DatatypeProperty structure =
                ont.getDatatypeProperty(nsName + "structure-type");
        DatatypeProperty sort =
                ont.getDatatypeProperty(nsName + "sort");

        int count =
                0;


        String instance =
                "activity_structure_instance_" +
                (String.valueOf(lastInst.lastInstance(parent)));

        Individual instLe =
                ont.createIndividual(nsName + instance, parent);
        instLD.setPropertyValue(exeEntRef, instLe);

        //add the Attributes to the new instance
        NodeList learActiChildren =
                node.getChildNodes();
        if (learActiChildren != null) {
            for (int i =
                    0; i < learActiChildren.getLength(); i++) {
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:title") == 0) {
                    String leTitle =
                            learActiChildren.item(i).getTextContent();
                    try {
                        RDFNode titleValue =
                                ont.createTypedLiteral(leTitle);
                        instLe.setPropertyValue(title, titleValue);

                        NamedNodeMap attrsItem =
                                node.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(identifier,
                                        identifierValue);
                            }
                            if (attr.getNodeName().trim().compareTo("number-to-select") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
                                RDFNode numberValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(number, numberValue);
                            }
                            if (attr.getNodeName().trim().compareTo("sort") == 0) {
                                RDFNode sortValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(sort, sortValue);
                            }
                            if (attr.getNodeName().trim().compareTo("structure-type") ==
                                    0) {
                                RDFNode structureValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(structure,
                                        structureValue);
                            }
                        }//for                   
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                }// Information element: create the information instance
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:information") == 0) {
                    OntClass information =
                            ont.getOntClass(nsName + "Information");

                    DatatypeProperty identifierRef =
                            ont.getDatatypeProperty(nsName + "identifier-ref");
                    DatatypeProperty isVisible =
                            ont.getDatatypeProperty(nsName + "isvisible");
                    ObjectProperty metadataRef =
                            ont.getObjectProperty(nsName + "metadata-ref");


                    NodeList inforChildren =
                            learActiChildren.item(i).getChildNodes();
                    lastInst =
                            new lastInstanceSLDTranslator();
                    instance =
                            "information_instance_" +
                            (String.valueOf(lastInst.lastInstance(information)));

                    Individual instInf =
                            ont.createIndividual(nsName + instance, information);
                    ObjectProperty informationRef =
                            ont.getObjectProperty(nsName + "information-ref");
                    instLe.setPropertyValue(informationRef, instInf);

                    //Properties about information class
                    for (int x =
                            0; x < inforChildren.getLength(); x++) {
                        try {
                            if (inforChildren.item(x).getNodeName().
                                    compareTo("imsld:title") == 0) {
                                String inforTitle =
                                        inforChildren.item(x).getNodeValue();
                                RDFNode titleValue =
                                        ont.createTypedLiteral(title, inforTitle);

                            }
                        } catch (DOMException ex) {
                            ex.printStackTrace();
                        }

                        if (inforChildren.item(x).getNodeName().
                                compareTo("imsld:item") == 0) {
                            NamedNodeMap attrsInf =
                                    inforChildren.item(x).getAttributes();
                            for (int y =
                                    0; y < attrsInf.getLength(); y++) {
                                Node attr =
                                        attrsInf.item(y);
                                try {
                                    if (attr.getNodeName().trim().
                                            compareTo("identifier") == 0) {

                                        TypeMapper tm =
                                                TypeMapper.getInstance();
                                        RDFDatatype overType =
                                                tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                        RDFNode identifierValue =
                                                ont.createTypedLiteral(attr.getNodeValue().
                                                trim(), overType);
                                        instInf.setPropertyValue(identifier,
                                                identifierValue);
                                    }
                                    if (attr.getNodeName().trim().compareTo("is-visible") ==
                                            0) {
                                        RDFNode visibleValue;
                                        if (attr.getNodeValue().trim().compareTo("true") ==
                                                0) {
                                            visibleValue =
                                                    ont.createTypedLiteral(true);
                                        } else {
                                            visibleValue =
                                                    ont.createTypedLiteral(false);
                                        }

                                        instInf.setPropertyValue(isVisible,
                                                visibleValue);
                                    }
                                    if (attr.getNodeName().trim().
                                            compareTo("metadata-LOM-ref") == 0) {
                                        TypeMapper tm =
                                                TypeMapper.getInstance();
                                        RDFDatatype overType =
                                                tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#Metadata");
                                        RDFNode metadataRefValue =
                                                ont.createTypedLiteral(attr.getNodeValue().
                                                trim(), overType);
                                        instInf.setPropertyValue(metadataRef,
                                                metadataRefValue);

                                    }
                                    if (attr.getNodeName().trim().
                                            compareTo("identifierref") == 0) {
                                        RDFNode identifierRefValue =
                                                ont.createTypedLiteral(attr.getNodeValue().
                                                trim());
                                        instInf.setPropertyValue(identifierRef,
                                                identifierRefValue);

                                    }
                                } catch (DOMException ex) {
                                    ex.printStackTrace();
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }//end if item      
                    } //end for           
                }//end if information                                               




                //Environment refTranslator attribute relation                      
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:environment-ref") == 0) {
                    NamedNodeMap attrsItem =
                            learActiChildren.item(i).getAttributes();
                    for (int x =
                            0; x < attrsItem.getLength(); x++) {
                        Node attr =
                                attrsItem.item(x);
                        if (attr.getNodeName().trim().compareTo("ref") == 0) {
                            try {
                                refTranslator reference =
                                        new refTranslator();
                                reference.inst =
                                        instLe;
                                reference.instRef =
                                        attr.getNodeValue().trim();
                                referencesEnvironment.add(reference);

                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:learning-activity-ref") == 0) {
                    OntClass learningActivity =
                            ont.getOntClass(nsName + "Learning-Activity");

                    ObjectProperty learActiRef =
                            ont.getObjectProperty(nsName +
                            "learning-activity-ref");

                    NamedNodeMap atttr =
                            learActiChildren.item(i).getAttributes();
                    for (int a =
                            0; a < atttr.getLength(); a++) {
                        Node att =
                                atttr.item(a);
                        if (att.getNodeName().trim().compareTo("ref") == 0) {
                            refTranslator reference =
                                    new refTranslator();
                            reference.inst =
                                    instLe;
                            reference.instRef =
                                    att.getNodeValue().trim();
                            referencesLearningActivity.add(reference);
                        }
                    }

                }
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:support-activity-ref") == 0) {
                    OntClass SupportActivity =
                            ont.getOntClass(nsName + "Support-Activity");
                    ObjectProperty supActRef =
                            ont.getObjectProperty(nsName +
                            "support-activity-ref");

                    NamedNodeMap atttr =
                            learActiChildren.item(i).getAttributes();
                    for (int a =
                            0; a < atttr.getLength(); a++) {
                        Node att =
                                atttr.item(a);
                        if (att.getNodeName().trim().compareTo("ref") == 0) {
                            TypeMapper tm =
                                    TypeMapper.getInstance();
                            RDFDatatype overType =
                                    tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                            RDFNode supportRefValue =
                                    ont.createTypedLiteral(att.getNodeValue().
                                    trim(), overType);
                            searchInstance searchInst =
                                    new searchInstance();
                            Individual instSupportActivity =
                                    searchInst.search(SupportActivity,
                                    identifier, supportRefValue);
                            instLe.addProperty(supActRef, instSupportActivity);


                            refTranslator reference =
                                    new refTranslator();
                            reference.inst =
                                    instLe;
                            reference.instRef =
                                    att.getNodeValue().trim();
                            references.add(reference);
                        }

                    }
                }
//             if (learActiChildren.item(i).getNodeName().
//                    compareTo("imsld:unit-of-learning-href")==0) {
//                OntClass actStruc=ont.getOntClass(nsName+"Activity-Structure");
//                
//                OntClass ActivityStructure=ont.getOntClass(nsName+"Activity-Structure");
//                ObjectProperty unitLearRef=ont.getObjectProperty(nsName+"unit-of-learning-href");                
//                
//                   NamedNodeMap atttr = learActiChildren.item(i).getAttributes();
//                         for (int a = 0; a < atttr.getLength(); a++){
//                            Node att = atttr.item(a);
//                            if (att.getNodeName().trim().compareTo("refTranslator")==0)
//                            {
//                                RDFNode unitLearRefValue=ont.createTypedLiteral(att.getNodeValue().trim());
//                                instLe.setPropertyValue(unitLearRef,unitLearRefValue);
//                            }
//                            
//                         }
//           }
                if (learActiChildren.item(i).getNodeName().
                        compareTo("imsld:activity-structure-ref") == 0) {
                    OntClass actStructure =
                            ont.getOntClass(nsName + "Activity-Structure");
                    ObjectProperty actStructRef =
                            ont.getObjectProperty(nsName +
                            "activity-structure-ref");

                    NamedNodeMap atttr =
                            learActiChildren.item(i).getAttributes();
                    for (int a =
                            0; a < atttr.getLength(); a++) {
                        Node att =
                                atttr.item(a);
                        if (att.getNodeName().trim().compareTo("ref") == 0) {

                            refTranslator reference =
                                    new refTranslator();
                            reference.inst =
                                    instLe;
                            reference.instRef =
                                    att.getNodeValue().trim();
                            references.add(reference);

                        }

                    }
                }
            }
        }

    }
}
